package com.zy.openchat.core.model.redpacket;

import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;
import com.zy.openchat.util.DateHelper;
import com.zy.openchat.util.TimeConvertUtils;

import java.util.ArrayList;
import java.util.List;

public class RedPacketInfo implements Parcelable {
    //红包标识
    public long redPacketId;
    //归属chat
    public long chatId;
    //1.单聊红包 2.拼手气红包 3.普通红包
    public int type;
    //标题
    public String title;
    //单个红包金额
    public float price;
    //红包总金额
    public float total_price;
    //红包数量
    public int count;
    //红包创建人
    public long from;
    //支付密码-仅创建红包时有效
    //钱包密码(MD5)-使用Common.MD5()方法
    public String password;
    //以上属性红包创建时赋值

    //创建时间
    public long createAt;
    //是否过期
    public boolean isExpire;
    //红包领取列表-RedPacketPickUser
    public List<RedPacketPickUser> users;

    public String getDate() {
        return TimeConvertUtils.formatDate9(createAt * 1000);
    }

    public String getTypeContent() {
        String typeContent = "";
        switch (type) {
            case 1:
                typeContent = "单聊红包";
                break;
            case 2:
                typeContent = "拼手气红包";
                break;
            case 3:
                typeContent = "普通红包";
                break;
            default:
                typeContent = "未知红包";
                break;
        }
        return typeContent;
    }

    public boolean isDone() {
        if (type == 1) {//单聊红包
            return users != null && users.size() >= 1;
        }
        return users != null && users.size() >= count;
    }

    private boolean isGot() {
        if (users != null && users.size() > 0) {
            for (RedPacketPickUser user : users) {
                if (user.userId == UserInfo.getUserInfo().id) {
                    return true;
                }
            }
        }
        return false;
    }

    public TelegramManager.RpState getRpState() {
        if (isGot()) {
            if (isDone()) {
                return TelegramManager.RpState.RpState_GotADone;
            } else {
                return TelegramManager.RpState.RpState_Got;
            }
        } else {
            if (isDone()) {
                return TelegramManager.RpState.RpState_Done;
            } else {
                if (isExpire) {
                    return TelegramManager.RpState.RpState_Expire;
                } else {
                    return TelegramManager.RpState.RpState_To_Get;
                }
            }
        }
    }

    public float bestPrice() {
        float best = 0.0f;
        if (users != null) {
            for (RedPacketPickUser user : users) {
                if (user.price > best) {
                    best = user.price;
                }
            }
        }
        return best;
    }

    public long bestUserId() {
        long bestUserId = -1;
        float best = 0.0f;
        if (users != null) {
            for (RedPacketPickUser user : users) {
                if (user.price > best) {
                    best = user.price;
                    bestUserId = user.userId;
                }
            }
        }
        return bestUserId;
    }

    public RedPacketPickUser curUserRp() {
        if (users != null) {
            for (RedPacketPickUser user : users) {
                if (user.userId == UserInfo.getUserInfo().id) {
                    return user;
                }
            }
        }
        return null;
    }

    public String getStatusDes() {
        return String.format("已领完 %d/%d", users != null ? users.size() : 0, count);
    }

    public String getRedPacketPrice() {
        return String.format("+%.2f元", curUserRpPrice());
    }

    public String getRedPacketSendPrice() {
        return String.format("-%.2f元", total_price);
    }

    public float curUserRpPrice() {
        float price = 0.00f;
        if (users != null) {
            for (RedPacketPickUser user : users) {
                if (user.userId == UserInfo.getUserInfo().id) {
                    price = user.price;
                    return price;
                }
            }
        }
        return price;
    }

    private float totalGotPrice() {
        float total = 0.0f;
        if (users != null) {
            for (RedPacketPickUser user : users) {
                total += user.price;
            }
        }
        return total;
    }

    private int doneTotalSeconds() {
        long gotAt = 0;
        if (users != null) {
            for (RedPacketPickUser user : users) {
                if (user.gotAt > gotAt) {
                    gotAt = user.gotAt;
                }
            }
        }
        return (int) (gotAt - createAt);
    }

    @SuppressLint("DefaultLocale")
    public String detailDes() {
        if (type == 1) {//单聊红包
            if (users != null && users.size() >= 1) {//已被领取
                if (from == UserInfo.getUserInfo().id) {
                    return String.format("红包金额%s元，对方已领取", Common.formatPrice(total_price));
                } else {
                    return String.format("红包金额%s元，已领取", Common.formatPrice(total_price));
                }
            } else {//未被领取
                if (isExpire) {
                    return String.format("红包金额%s元，已过期", Common.formatPrice(total_price));
                } else {
                    if (from == UserInfo.getUserInfo().id) {
                        return String.format("红包金额%s元，等待对方领取", Common.formatPrice(total_price));
                    } else {
                        return String.format("红包金额%s元，等待领取", Common.formatPrice(total_price));
                    }
                }
            }
        } else {//群组红包-拼手气或者普通红包
            if (users != null && users.size() >= count) {//领光
                return String.format("%d个红包共%s元，%s被抢光", count, Common.formatPrice(total_price), DateHelper.timeFormattedForRp(doneTotalSeconds()));
            } else {//未领完
                int gotCount = users != null ? users.size() : 0;
                if (isExpire) {
                    return String.format("该红包已过期。已领取%d/%d个，共%s/%s元", gotCount, count, Common.formatPrice(totalGotPrice()), Common.formatPrice(total_price));
                } else {
                    if (from == UserInfo.getUserInfo().id) {
                        return String.format("已领取%d/%d个，共%s/%s元", gotCount, count, Common.formatPrice(totalGotPrice()), Common.formatPrice(total_price));
                    } else {
                        return String.format("已领取%d/%d个", gotCount, count);
                    }
                }
            }
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.redPacketId);
        dest.writeLong(this.chatId);
        dest.writeInt(this.type);
        dest.writeString(this.title);
        dest.writeFloat(this.price);
        dest.writeFloat(this.total_price);
        dest.writeInt(this.count);
        dest.writeLong(this.from);
        dest.writeString(this.password);
        dest.writeLong(this.createAt);
        dest.writeByte(this.isExpire ? (byte) 1 : (byte) 0);
        dest.writeList(this.users);
    }

    public RedPacketInfo() {
    }

    protected RedPacketInfo(Parcel in) {
        this.redPacketId = in.readLong();
        this.chatId = in.readLong();
        this.type = in.readInt();
        this.title = in.readString();
        this.price = in.readFloat();
        this.total_price = in.readFloat();
        this.count = in.readInt();
        this.from = in.readLong();
        this.password = in.readString();
        this.createAt = in.readLong();
        this.isExpire = in.readByte() != 0;
        this.users = new ArrayList<RedPacketPickUser>();
        in.readList(this.users, RedPacketPickUser.class.getClassLoader());
    }

    public static final Creator<RedPacketInfo> CREATOR = new Creator<RedPacketInfo>() {
        @Override
        public RedPacketInfo createFromParcel(Parcel source) {
            return new RedPacketInfo(source);
        }

        @Override
        public RedPacketInfo[] newArray(int size) {
            return new RedPacketInfo[size];
        }
    };
}
